﻿using Dapper;
using Finance.Api.Read.Applications.Commands.财务管理;
using Finance.Api.Read.Dtos;
using Finance.Domain;
using Finance.ErrorCode;
using Finance.Infrastructure;
using MediatR;
using System.Text;

namespace Finance.Api.Read.Applications.CommandHands.财务管理
{
    public class ReturnMoneyQueryCommandHand : IRequestHandler<ReturnMoneyQueryCommand, ApiResult<ApiPaging<List<ReturnMoneyDto>>>>
    {
        private readonly IDapperDb _db;

        public ReturnMoneyQueryCommandHand(IDapperDb db)
        {
            _db = db;
        }

        public async Task<ApiResult<ApiPaging<List<ReturnMoneyDto>>>> Handle(ReturnMoneyQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiResult<ApiPaging<List<ReturnMoneyDto>>>();
            StringBuilder stringBuilders = new StringBuilder();
            StringBuilder Totalnum = new StringBuilder();
            ApiPaging<List<ReturnMoneyDto>> res = new ApiPaging<List<ReturnMoneyDto>>();
            var parmenters = new DynamicParameters();

            stringBuilders.Append(@"SELECT a.Id,
                                        a.SalesRefundNo,
                                        a.CustomerId,
                                        b.CustomerName As CustomName,
                                        a.SupplieId,
                                        c.SuppliersName As SupplieName,
                                        a.RefundSybject,
                                        a.RefundAmount,
                                        a.RefundDate,
                                        a.RefundPerson,
                                        a.RefundProgess,
                                        a.refundstyle,
                                        a.CreateName,
                                        a.CreateTime,
                                        a.UpdateName,
                                        a.UpdateTime,
                                        a.DeleteName,
                                        a.DeleteTime
                                FROM FinanceReturnMoney a
                                JOIN Customer b
                                    ON a.CustomerId=b.Id
                                JOIN SuppliersModel c
                                    ON a.SupplieId=c.Id
                                WHERE 1=1
                                        AND a.IsDelete=0");
            if (!string.IsNullOrEmpty(request.CustomName))
            {
                stringBuilders.Append(" and b.CustomName LIKE @CustomName ");
                parmenters.Add("CustomName", $"%{request.CustomName}%");

            }
            if (!string.IsNullOrEmpty(request.SupplieName))
            {
                stringBuilders.Append("and c.SuppliersName LIKE @SupplieName");
                parmenters.Add("SupplieName", $"%{request.SupplieName}%");
            }
            if (request.style > 0)
            {

                stringBuilders.Append("and a.RefundProgess = @progess");
                parmenters.Add("progess", $"{request.style}");
            }
            Totalnum.Append(@"SELECT COUNT(1)
                            FROM FinanceReturnMoney ");
            if (!string.IsNullOrEmpty(request.CustomName))
            {
                stringBuilders.Append(" and b.CustomName LIKE @CustomName ");


            }
            if (!string.IsNullOrEmpty(request.SupplieName))
            {
                stringBuilders.Append("and c.SuppliersName LIKE @SuppliersName");

            }
            int totalcount = await _db.QueryFirst<int>(Totalnum.ToString(), parmenters);
            stringBuilders.Append($"order by a.Id DESC offset ({request.index - 1})*{request.size} rows fetch next {request.size} rows only");


            var list = await _db.Query<ReturnMoneyDto>(stringBuilders.ToString(), parmenters);
            res.data = list.ToList();
            res.totalcount = totalcount;
            res.totalpcount = (int)Math.Ceiling(totalcount * 1.0 / request.size);
            result.data = res;
            result.message = "分页查询成功";
            result.Code = Enumcode.ok;



            return await Task.FromResult(result);
        }
    }
}
